package com.yuncheng.spcyApi.controller;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.SpcyRsWjmb;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.entity.SpcyRsWjmbZjml;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.hbjc.HbjcbgMlVo;
import com.yuncheng.spcyApi.vo.wjmb.SelectSqsxWjmbVo;
import com.yuncheng.spcyApi.vo.wjmb.SqsxJcbgWjmbVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 文件模板-章节目录
 * @Author: yuncheng
 * @Date: 2024-04-28
 * @Version: V1.0
 */
@Api(tags = "文件模板-章节目录接口")
@RestController
@RequestMapping("/api/spzx-spcy/mbzj")
public class SpcyRsWjmbZjmlController {

    private static final Logger log = LoggerFactory.getLogger(SpcyRsWjmbZjmlController.class);

    @Resource
    private ISpcyRsWjmbService spcyRsWjmbService;

    @Resource
    private ISpcyRsWjmbZjmlService spcyRsWjmbZjmlService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private ISpcyRsYwzdxxService spcyRsYwzdxxService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Log(title = "获取现场检查情况描述-整体描述和情况汇总标题")
    @ApiOperation(value = "获取现场检查情况描述-整体描述和情况汇总标题")
    @GetMapping(value = "/mbMsJcxmTitle")
    public HttpResult<?> mbMsJcxmTitle(@RequestParam(value = "sqsxid",defaultValue = "AA") String sqsxid) {

        return HttpResult.ok(spcyRsWjmbService.selectJcxmTitleByFsqsxId(sqsxid));
    }

    @Log(title = "文件模板库-只查询现场检查报告相关模板-分页列表")
    @ApiOperation(value = "文件模板库-只查询现场检查报告相关模板-分页列表")
    @GetMapping(value = "queryBgmbkList")
    public HttpResult queryBgmbkList(@RequestParam(value = "id",defaultValue = "") String id,
                                     @RequestParam(value = "fmbbt",defaultValue = "") String fmbbt,
                                     PageVo pageVo){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(id);
        if (wjmb == null){
            return HttpResult.error("没有相匹配的模版数据！");
        }

        String fywflcode = "";
        if (StringUtils.isNotBlank(wjmb.getFywflcode())){
            fywflcode = wjmb.getFywflcode();
        }else if (StringUtils.isNotBlank(wjmb.getFsqsxid())){
            fywflcode = spcySqsxBaiscService.getYwflCodeBySqsxId(wjmb.getFsqsxid());
        }

        Page<SpcyRsWjmb> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        return HttpResult.ok(spcyRsWjmbService.page(page, new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                .eq(SpcyRsWjmb::getFmblx1, WjmlConstant.xcjcbg)
                .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)));
    }

    @Log(title = "文件模板-章节目录-分页列表查询")
    @ApiOperation(value = "文件模板-章节目录-分页列表查询")
    @GetMapping(value = "/queryMbZjList")
    public HttpResult<?> queryMbZjList(SpcyRsWjmbZjml query, PageVo pageVo) {
        if (StringUtils.isBlank(query.getFwjmbid())){
            return HttpResult.error("模板参数为空！");
        }

        return HttpResult.ok(spcyRsWjmbZjmlService.queryPageList(pageVo, query));
    }

    @Log(title = "更新方案制定-检查报告模板内容")
    @ApiOperation(value = "更新方案制定-检查报告模板内容")
    @RequestMapping(value = "updateJcmb",method = {RequestMethod.POST, RequestMethod.PUT})
    public HttpResult updateJcmb(@RequestBody HashMap<String,Object> parVo){

        String id = "";
        try {
            id = parVo.get("id").toString();
        }catch (Exception e){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmb update = spcyRsWjmbService.getById(id);
        if (update == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update, parVo);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规法规名称出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规法规名称出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyRsWjmbService.updateById(update);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "获取PC-任务报告模板列表")
    @ApiOperation(value = "获取PC-任务报告模板列表")
    @GetMapping(value = "/queryjcbgMbList")
    public HttpResult<?> queryjcbgMbList(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid) {
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<SpcyRsWjmb> list = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                .orderByAsc(SpcyRsWjmb::getCreateTime));

        List<SqsxJcbgWjmbVo> resultList = new ArrayList<>();
        for (SpcyRsWjmb bean : list){
            SqsxJcbgWjmbVo newBean = new SqsxJcbgWjmbVo();
            BeanUtil.copyProperties(bean, newBean);
            newBean.setFwjxsmc(bean.getFmbbt());

            resultList.add(newBean);
        }

        return HttpResult.ok(resultList);
    }

    @Log(title = "获取APP-任务报告模板列表")
    @ApiOperation(value = "获取APP-任务报告模板列表")
    @GetMapping(value = "/appRwbgmbList")
    public HttpResult<?> appRwbgmbList(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid) {
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<SpcyRsWjmb> list = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                .orderByAsc(SpcyRsWjmb::getCreateTime));

        List<SqsxJcbgWjmbVo> resultList = new ArrayList<>();
        for (SpcyRsWjmb bean : list){
            SqsxJcbgWjmbVo newBean = new SqsxJcbgWjmbVo();
            BeanUtil.copyProperties(bean, newBean);
            newBean.setFwjxsmc(bean.getFmbbt());

            resultList.add(newBean);
        }

        Map dataMap = new HashMap();
        dataMap.put("clmc", "任务报告模板");
        dataMap.put("wjList", resultList);
        return HttpResult.ok(dataMap);
    }

    @Log(title = "保存方案制作-选择检查报告-文件模板")
    @ApiOperation(value = "保存方案制作-选择检查报告-文件模板")
    @PostMapping(value = "/saveSelectSqsxWjmb")
    public HttpResult<?> saveSelectSqsxWjmb(@RequestBody SelectSqsxWjmbVo bean) {
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("模板参数为空！");
        }
        if (StringUtils.isBlank(bean.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }

        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(bean.getId());
        if (wjmb == null){
            return HttpResult.error("没有想匹配的模板数据！");
        }
        String newSqsxWjmbid = GetUuIdUtils.ReplaceUuId();
        boolean flag = this.copyWjmbToSqsx(wjmb, newSqsxWjmbid, bean.getSqsxid());
        if (flag) {
            this.copyWjmbZj(bean.getId(), newSqsxWjmbid, bean.getSqsxid());
        }

        return HttpResult.ok("操作成功");
    }

    @Log(title = "合并事项方案制作-保存选择检查报告模板")
    @ApiOperation(value = "合并事项方案制作-保存选择检查报告模板")
    @PostMapping(value = "/hbxzbgmb")
    public HttpResult<?> hbxzbgmb(@RequestBody SelectSqsxWjmbVo paramVo) {
        if (StringUtils.isBlank(paramVo.getId())){
            return HttpResult.error("模板参数为空！");
        }
        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(paramVo.getBxzsqsxid())){
            return HttpResult.error("请选择对应事项参数！");
        }
        if (StringUtils.isBlank(paramVo.getBgmc())){
            return HttpResult.error("请填写报告名称！");
        }

        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(paramVo.getId());
        if (wjmb == null){
            return HttpResult.error("没有想匹配的模板数据！");
        }
        String newSqsxWjmbid = GetUuIdUtils.ReplaceUuId();

        wjmb.setFmbbt(paramVo.getBgmc());
        boolean flag = this.copyWjmbToSqsx(wjmb, newSqsxWjmbid, paramVo.getSqsxid(), paramVo.getBxzsqsxid());
        if (flag) {
            this.copyWjmbZj(paramVo.getId(), newSqsxWjmbid, paramVo.getSqsxid());
        }

        return HttpResult.ok("操作成功");
    }

    @Log(title = "合并事项现场检查-检查报告目录")
    @ApiOperation(value = "合并事项现场检查-检查报告目录")
    @GetMapping(value = "/queryHbjcBgMl")
    public HttpResult<?> queryHbjcBgMl(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid) {
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<HbjcbgMlVo> resultList = new ArrayList<>();

        List<SpcyRsWjmb> wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                .isNotNull(SpcyRsWjmb::getFmblx3)
                .orderByAsc(SpcyRsWjmb::getCreateTime));
        int order = 1;
        for (SpcyRsWjmb wjmb : wjmbList){
            /** 存在两个合并事项,默认取第一个 */
            if (wjmb.getFmblx3().contains(",") && StringPatternUtils.getStringsByIds(wjmb.getFmblx3()).length>1) {

                String bhbsxid = StringPatternUtils.getStringsByIds(wjmb.getFmblx3())[0];
                resultList.add(new HbjcbgMlVo(bhbsxid, order, bhbsxid, wjmb.getFmbbt()));
            }else {
                resultList.add(new HbjcbgMlVo(wjmb.getFmblx3(), order, wjmb.getFmblx3(), wjmb.getFmbbt()));
            }

            order++;
        }

        return HttpResultVo.HttpResultList(resultList);
    }

    /** 复制文件模板 */
    private boolean copyWjmbToSqsx(SpcyRsWjmb wjmb, String newSqsxWjmbid, String sqsxid){
        return this.copyWjmbToSqsx(wjmb, newSqsxWjmbid, sqsxid, "");
    }
    private boolean copyWjmbToSqsx(SpcyRsWjmb wjmb, String newSqsxWjmbid, String sqsxid, String bxzsqsxid){
        SpcyRsWjmb sqsxWjmb = new SpcyRsWjmb();
        sqsxWjmb.setId(newSqsxWjmbid);
        sqsxWjmb.setFsqsxid(sqsxid);

        sqsxWjmb.setFmbbt(wjmb.getFmbbt());
        sqsxWjmb.setFmblx1(wjmb.getFmblx1());
        sqsxWjmb.setFmblx2(wjmb.getFmblx2());
        sqsxWjmb.setFmsmbnr(wjmb.getFmsmbnr());

        /** 存在两个被合并事项 */
        if (StringUtils.isNotBlank(bxzsqsxid) && bxzsqsxid.contains(",") && StringPatternUtils.getStringsByIds(bxzsqsxid).length>1) {
            sqsxWjmb.setFbz(SpcyCommonConstant.QY_WJCL_FBZ_HBSX); /** 被选择合并事项id */
        }
        sqsxWjmb.setFmblx3(bxzsqsxid);

        sqsxWjmb.setFwjlx(wjmb.getFwjlx());
        sqsxWjmb.setFwjccmc(sqsxid+"-"+GetUuIdUtils.ReplaceUuId() + wjmb.getFwjlx());

        String wjml = "/sqsx" + DateConversionUtils.DateToMonthTwo(new Date());
        File dir = new File(localFileUploadUtils.getWjmbAbsUrl(wjml));
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String newCflj = wjml + "/" + sqsxWjmb.getFwjccmc();
        sqsxWjmb.setFcflj(newCflj);
        boolean flag = FileCopyUtils.copyFile(localFileUploadUtils.getWjmbAbsUrl(wjmb.getFcflj()), localFileUploadUtils.getWjmbAbsUrl(newCflj));
        if (!flag){
            return false;
        }

        if (StringUtils.isBlank(wjmb.getFmsmbbt())){
            sqsxWjmb.setFmsmbbt(SpcyCommonConstant.WJMB_MSMBNR_TITLE);
        }else {
            sqsxWjmb.setFmsmbbt(wjmb.getFmsmbbt());
        }
        if (StringUtils.isBlank(wjmb.getFjcxmbt())){
            sqsxWjmb.setFjcxmbt(SpcyCommonConstant.WJMB_JCXM_TITLE);
        }else {
            sqsxWjmb.setFjcxmbt(wjmb.getFjcxmbt());
        }

        sqsxWjmb.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        sqsxWjmb.setCreateTime(new Date());
        spcyRsWjmbService.save(sqsxWjmb);

        return true;
    }

    /** 复制模板章节 */
    private void copyWjmbZj(String wjmbid, String newSqsxWjmbid, String sqsxid){
        List<SpcyRsWjmbZjml> wjmbZjList = spcyRsWjmbZjmlService.list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, wjmbid));

        List<SpcyRsWjmbZjml> newWjmbZjList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(wjmbZjList)) {
            for (SpcyRsWjmbZjml copyBean : wjmbZjList) {
                SpcyRsWjmbZjml bean = new SpcyRsWjmbZjml();
                BeanUtil.copyProperties(copyBean, bean);
                bean.setId(GetUuIdUtils.NotReplaceUuId());
                bean.setFwjmbid(newSqsxWjmbid);
                bean.setFsqsxid(sqsxid);

                newWjmbZjList.add(bean);
            }
        }else {
            List<String> zjList = StringPatternUtils.getListByIds(
                    spcyRsYwzdxxService.querySqsxDefaultJczj(
                            spcySqsxBaiscService.getYwflCodeBySqsxId(sqsxid)
                    )
            );

            for (int i = 0; i < zjList.size(); i++){
                SpcyRsWjmbZjml bean = new SpcyRsWjmbZjml();
                bean.setId(GetUuIdUtils.NotReplaceUuId());
                bean.setFwjmbid(newSqsxWjmbid);
                bean.setFsqsxid(sqsxid);
                bean.setFzjmc(zjList.get(i));
                bean.setForder(i+1);

                newWjmbZjList.add(bean);
            }
        }

        spcyRsWjmbZjmlService.saveBatch(newWjmbZjList);
    }

    @Log(title = "检查报告模板-删除")
    @ApiOperation(value = "检查报告模板-删除")
    @DeleteMapping(value = "/deleteWjmbById")
    public HttpResult<?> deleteWjmbById(@RequestParam(value = "id",defaultValue = "") String id) {
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("参数为空");
        }

        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(id);
        if (wjmb == null){
            return HttpResult.error("没有想匹配的模板数据！");
        }

        boolean flag = spcyRsWjmbService.removeById(id);
        if (flag) {
            localFileUploadUtils.deleteLocalWjmbFileUrl(wjmb.getFcflj()); // 删除文件

            String operContent = "删除文件模板信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjmb);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_RS_WJMB, operContent, id, "删除文件模板", SpcyConstant.LOGS_SYS_TYPE_SC);
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "文件模板-章节目录-添加")
    @ApiOperation(value = "文件模板-章节目录-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody SpcyRsWjmbZjml bean) {
        if (StringUtils.isBlank(bean.getFwjmbid())){
            return HttpResult.error("模板参数为空！");
        }
        if (StringUtils.isBlank(bean.getFzjmc())){
            return HttpResult.error("名称为空！");
        }

        int order = spcyRsWjmbZjmlService.list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, bean.getFwjmbid()))
                .size();
        bean.setForder(order + 1);


        spcyRsWjmbZjmlService.save(bean);

        return HttpResult.ok("添加成功");
    }

    @Log(title = "文件模板-章节目录-修改")
    @ApiOperation(value = "文件模板-章节目录-修改")
    @PostMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody HashMap<String,Object> map) {
        String id = (String)map.get("id");
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmbZjml updateBean = spcyRsWjmbZjmlService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        Integer oldOrder = updateBean.getForder(); // 旧的排序值

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("出错-文件模板-章节目录-修改【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-文件模板-章节目录-修改【调用TargetException】,{}",e.getMessage());
        }

        updateBean.setForder(oldOrder); // 不允许修改排序值
        spcyRsWjmbZjmlService.updateById(updateBean);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "文件模板-章节目录-通过id删除")
    @ApiOperation(value = "文件模板-章节目录-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam(value = "id",defaultValue = "") String id) {
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmbZjml bean = spcyRsWjmbZjmlService.getById(id);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        boolean flag = spcyRsWjmbZjmlService.removeById(id);
        if (flag){
            this.zjReloadOrderInDel(id, bean);
        }

        String operContent = "删除文件模板章节信息：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(bean);
        operContent += LogUtils.MapToStringMethod(oldMap);
        spcyLogService.addLogs(LogConstant.LOGS_RS_WJMB, operContent, id, "删除文件模板章节", SpcyConstant.LOGS_SYS_TYPE_SC);


        return HttpResult.ok("删除成功");
    }

    @Log(title = "文件模板-章节目录-通过id查询")
    @ApiOperation(value = "文件模板-章节目录-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam(value = "id",defaultValue = "") String id) {

        return HttpResultVo.HttpResultSave(spcyRsWjmbZjmlService.getById(id));
    }

    @Log(title = "文件模板-章节目录-向上排序")
    @ApiOperation(value = "文件模板-章节目录-向上排序")
    @GetMapping(value = "/mbZjUpOrder")
    public HttpResult<?> mbZjUpOrder(@RequestParam(value = "id",defaultValue = "") String id) {
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmbZjml bean = spcyRsWjmbZjmlService.getById(id);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        this.zjUpOrDownOrder(id, bean, true, false);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "文件模板-章节目录-向下排序")
    @ApiOperation(value = "文件模板-章节目录-向下排序")
    @GetMapping(value = "/mbZjDownOrder")
    public HttpResult<?> mbZjDownOrder(@RequestParam(value = "id",defaultValue = "") String id) {
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyRsWjmbZjml bean = spcyRsWjmbZjmlService.getById(id);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        this.zjUpOrDownOrder(id, bean, false, true);

        return HttpResult.ok("操作成功");
    }

    /** 向上排序/向下排序
     * @param indexId 修改的章节id
     * @param mbBean 修改的的章节
     * */
    private void zjUpOrDownOrder(String indexId, SpcyRsWjmbZjml mbBean, boolean isUpOrder, boolean isDownOrder){
        // 当前需要向上/向下排序的值
        if (mbBean == null) {
            mbBean = spcyRsWjmbZjmlService.getById(indexId);
            if (mbBean == null) {
                return;
            }
        }

        List<SpcyRsWjmbZjml> orderList = spcyRsWjmbZjmlService.list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, mbBean.getFwjmbid())
                .orderByAsc(SpcyRsWjmbZjml::getForder));

        int totalIndex = orderList.size(); // 总数

        int orderIndex = 0; // 列表下标

        int isDownOrderIndex = -1;  // 向下排序-坐标
        for (SpcyRsWjmbZjml nowBean : orderList){

            // 向上排序
            if (isUpOrder) {
                if ((orderIndex + 1) < totalIndex && orderList.get(orderIndex + 1).getId().equals(indexId)) {
                    nowBean.setForder(orderIndex + 2);
                } else if (nowBean.getId().equals(indexId)) {
                    if (orderIndex == 0){
                        nowBean.setForder(1);
                    }else {
                        nowBean.setForder(orderIndex);
                    }
                } else {
                    nowBean.setForder(orderIndex + 1);
                }
            }

            // 向下排序
            if (isDownOrder){
                if (mbBean.getId().equals(nowBean.getId())){
                    isDownOrderIndex = orderIndex;

                    // 临界值-为最后一个的情况下
                    if ( (isDownOrderIndex + 1) >= totalIndex){
                        nowBean.setForder(orderIndex + 1);
                    }else {
                        nowBean.setForder(orderIndex + 2);
                    }

                }else if ( isDownOrderIndex >= 0 && (isDownOrderIndex+1) == orderIndex){
                    nowBean.setForder(orderIndex);

                }else {
                    nowBean.setForder(orderIndex + 1);
                }
            }

            orderIndex++;
        }

        spcyRsWjmbZjmlService.updateBatchById(orderList);

    }

    /** 删除的时候，进行重新排序
     *  @param indexId 修改的章节id
     *  @param mbBean 修改的的章节
     * */
    private void zjReloadOrderInDel(String indexId, SpcyRsWjmbZjml mbBean){
        if (mbBean == null) {
            mbBean = spcyRsWjmbZjmlService.getById(indexId);
            if (mbBean == null) {
                return;
            }
        }

        List<SpcyRsWjmbZjml> orderList = spcyRsWjmbZjmlService.list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, mbBean.getFwjmbid())
                .orderByAsc(SpcyRsWjmbZjml::getForder));

        int orderIndex = 0; // 列表下标
        for (SpcyRsWjmbZjml nowBean : orderList){
            nowBean.setForder(orderIndex+1);
            orderIndex++;
        }

        spcyRsWjmbZjmlService.updateBatchById(orderList);
    }

    /** 更新排序值，进行重新排序
     *  @param indexId 修改的章节id
     *  @param mbBean 修改的的章节
     * */
    private void zjReloadOrderInUpdate(String indexId, SpcyRsWjmbZjml mbBean, Integer updateOrder){
        if (mbBean == null) {
            mbBean = spcyRsWjmbZjmlService.getById(indexId);
            if (mbBean == null) {
                return;
            }
        }

        List<SpcyRsWjmbZjml> orderList = spcyRsWjmbZjmlService.list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, mbBean.getFwjmbid())
                .orderByAsc(SpcyRsWjmbZjml::getForder));

        int totalIndex = orderList.size(); // 总数

        int orderIndex = 0; // 列表下标
        for (SpcyRsWjmbZjml nowBean : orderList){

            // 等于修改的值，更新的排序值>总数=排序最后
            if (nowBean.getId().equals(indexId)){
                if (updateOrder > totalIndex){
                    nowBean.setForder(totalIndex);
                }else {
                    nowBean.setForder(updateOrder);
                }
            }else {
                if (updateOrder > totalIndex && (orderIndex + 1) == totalIndex) {
                    nowBean.setForder(orderIndex);
                }else {
                    nowBean.setForder(orderIndex + 1);
                }
            }
        }

        spcyRsWjmbZjmlService.updateBatchById(orderList);
    }
}
